const app = getApp(),
  {
    $resHost
  } = app.globalData;
Component({
  properties: {
    wechatName: {
      type: String,
      value: ''
    },
    cardId: {
      type: String,
      value: ''
    },
    // 1 跳转到我的订单页面
    isHm: {
      type: String,
      value: ''
    }
  },
  data: {
    btnUrl: `${$resHost}/img/activity/bg.png`,
    cardId: '',
    createCardAppid: '',
    errInfo: '',
    openCardType: '',
    //开卡类型
    encrypt_card_id: '',
    biz: ''
  },
  attached() {
    app.$getSuccessData('/user/vip-card/get-open-card-params', {}, false).then(data => {
      this.setData({
        openCardType: data.type
      });
      if (data.type == '1') {
        this.setData({
          encrypt_card_id: this.getQueryVariable('encrypt_card_id', decodeURIComponent(data.url)),
          biz: this.getQueryVariable('biz', decodeURIComponent(data.url))
        });
      }
    });
  },
  methods: {
    handleClick() {
      if (this.data.openCardType == '0') {
        // 类型0不变
        this.defaultClick();
      }
      if (this.data.openCardType == '2') {
        this.fsmClick();
      }
      if (this.data.openCardType == '1') {
        this.oldGo(this.data.encrypt_card_id, this.data.biz);
      }
    },
    defaultClick() {
      app.$getSuccessData('/user/vip-card/get-master-id', {}, false).then(data => {
        xhs.navigateTo({
          url: `/pages/User/Goods/clubCard/${data.memberId == 0 ? 'clubCardOpen' : 'clubCard'}/index?${data.memberId == 0 ? 'id' : 'cardId'}=${data.cardId}&isHm=${this.data.isHm}`
        });
      });
    },
    fsmClick() {
      const {
        createCardAppid,
        cardId,
        errInfo
      } = this.data;
      if (errInfo) return xhs.showToast({
        icon: 'none',
        title: errInfo
      });
      createCardAppid ? this.go(createCardAppid, cardId) : this.getOpenCardParams();
    },
    getOpenCardParams() {
      xhs.showLoading();
      app.$getSuccessData('/user/vip-card/get-open-card-params', {}, false).then(data => {
        this.setData(data);
        this.go(data.createCardAppid, data.cardId);
      }).catch(errInfo => this.setData({
        errInfo
      })).finally(xhs.hideLoading);
    },
    go(create_card_appid, card_id) {
      const {
        shareEmployeeId,
        shareUserId
      } = app.getTrack();
      xhs.navigateToMiniProgram({
        appId: 'wxeb490c6f9b154ef9',
        path: 'pages/card_open/card_open',
        envVersion: 'release',
        extraData: {
          create_card_appid,
          card_id,
          outer_str: `${shareUserId}/${shareEmployeeId}`,
          activate_type: 'ACTIVATE_TYPE_NORMAL'
        }
      });
    },
    oldGo(encrypt_card_id, biz) {
      //1.0会员卡跳转
      xhs.navigateToMiniProgram({
        appId: 'wxeb490c6f9b154ef9',
        // 固定为此appid，不可改动   
        extraData: {
          outer_str: '',
          encrypt_card_id: encrypt_card_id,
          biz: biz
        },
        // 包括encrypt_card_id outer_str biz三个字段，须从step3中获得的链接中获取参数   
        success: function (res) {
          console.log(res);
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          console.log(res);
        }
      });
    },
    getQueryVariable(variable, url) {
      var vars = url.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return false;
    }
  }
});